package com.sandking.db.jedis;

import java.util.ArrayList;
import com.sandking.db.dao.XdjlDao;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import java.util.Set;
import redis.clients.jedis.Pipeline;
import com.sandking.db.bean.Xdjl;
import redis.clients.jedis.Jedis;

public class XdjlJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Xdjl getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Xdjl xdjl = null;
		try{
			String key = "Xdjl_Object";
			String field = SK_Plus.b("id:",id).e();
			xdjl = Xdjl.createForJson(jedis.hget(key,field));
			if(xdjl == null){
				xdjl = XdjlDao.getById(id);
				if(xdjl!=null){
					loadCache(xdjl);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return xdjl;
	}
	
	/**
	 * 根据( 用户id ) 查询
	 */
	public static List<Xdjl> getByYhid(int yhid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Xdjl> xdjls = new ArrayList<Xdjl>();
		try{
			String key = "Xdjl_Index";
			String field = SK_Plus.b(key,"yhid:",yhid).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Xdjl_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				xdjls = Xdjl.createForJson(jsons);
			}else{
				xdjls = XdjlDao.getByYhid(yhid);
				if(xdjls!=null && !xdjls.isEmpty()){
					loadCaches(xdjls);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return xdjls;
	}
	
	/**
	 * 根据( 目标id ) 查询
	 */
	public static List<Xdjl> getByMbid(int mbid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Xdjl> xdjls = new ArrayList<Xdjl>();
		try{
			String key = "Xdjl_Index";
			String field = SK_Plus.b(key,"mbid:",mbid).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Xdjl_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				xdjls = Xdjl.createForJson(jsons);
			}else{
				xdjls = XdjlDao.getByMbid(mbid);
				if(xdjls!=null && !xdjls.isEmpty()){
					loadCaches(xdjls);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return xdjls;
	}
	

	public static void loadCache(Xdjl xdjl){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(xdjl,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Xdjl xdjl,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Xdjl_Object";
		String field = SK_Plus.b("id:",xdjl.getId()).e();
		String data = xdjl.toJson();
		p.hset(key,field,data);
		
		String primaryKey = String.valueOf(xdjl.getId());
		key = "Xdjl_Index";
		// ---------------------- 聚集索引 ----------------------
		field = SK_Plus.b(key,"yhid:",xdjl.getYhid()).e();
		p.sadd(field, primaryKey);
		
		field = SK_Plus.b(key,"mbid:",xdjl.getMbid()).e();
		p.sadd(field, primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Xdjl> xdjls){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Xdjl xdjl : xdjls){
				loadCache(xdjl,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Xdjl xdjl){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(xdjl,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Xdjl xdjl,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Xdjl_Object";
		String field = SK_Plus.b("id:",xdjl.getId()).e();
		p.hdel(key,field);
		
		key = "Xdjl_Index";
		// ---------------------- 聚集索引 ----------------------
		String primaryKey = String.valueOf(xdjl.getId());
		field = SK_Plus.b(key,"yhid:",xdjl.getYhid()).e();
		p.srem(field, primaryKey);
		
		field = SK_Plus.b(key,"mbid:",xdjl.getMbid()).e();
		p.srem(field, primaryKey);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Xdjl> xdjls){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Xdjl xdjl : xdjls){
				clearCache(xdjl,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Xdjl insert(Xdjl xdjl){
		xdjl = XdjlDao.insert(xdjl);
		if(xdjl!=null){
			loadCache(xdjl);
		}
    	return xdjl;
    }
    
    public static Xdjl update(Xdjl xdjl){
    	xdjl = XdjlDao.update(xdjl);
    	if(xdjl!=null){
			loadCache(xdjl);
		}
    	return xdjl;
    }
    
    public static boolean delete(Xdjl xdjl){
    	boolean bool = XdjlDao.delete(xdjl);
    	if(bool){
    		clearCache(xdjl);
    	}
    	return bool;
    }
}